<?php

	require_once('../inc/fnc.php');
	require_once('../inc/conn.php');
	
	// Initializing variables
	$email = mysql_prep($_POST['email']);
	$pwd = sha1($_POST['pwd']);
	$userId = mysql_prep($_POST['userId']);
	
	if (!check_email_address($email)) {
		//Invalid Email
		//$return['error'] = "4"; // Return 4 for Invalid Email
		echo '{"error":"4"}';
	} else {
	
		// Checking whether the email is already registered or not			
		$query = "SELECT * FROM `users` WHERE `email` = '{$email}' LIMIT 1";
		$result = mysql_query( $query, $connection);
		if(!$result) { echo '{"error":"2"}'; } // Return 2 for database error
		
		//Check if the email already exist
		if (mysql_num_rows($result) == 1) { 
			while($row = mysql_fetch_array($result)) {
				$id = $row['userid'];
			}
			if ( $userId == $id ){
				echo json_encode(changeEmailPwd($userId,$email,$pwd)); // Return 1 for non existent account; 2 for database error; 3 for  successful update
			} else {
				//$return['error'] = "5"; // Return 5 for already existing user
				echo '{"error":"5"}';
			}
		} else {
			echo json_encode(changeEmailPwd($userId,$email,$pwd)); // Return 1 for non existent account; 2 for database error; 3 for  successful update
		}
	} // ENDS if (!check_email_address($email)) {

	
?>